package com.campus.mapper;

import com.campus.domain.entity.LostItemClaim;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 失物申领Mapper
 */
@Mapper
public interface LostItemClaimMapper {
    
    // 插入申领
    @Insert("INSERT INTO lost_item_claims(item_id, user_id, reason, contact_info, status) " +
            "VALUES(#{itemId}, #{userId}, #{reason}, #{contactInfo}, 0)")
    int insert(LostItemClaim claim);
    
    // 查询是否已申领（只检查待审核和已通过的申领，驳回的不算）
    @Select("SELECT COUNT(*) FROM lost_item_claims WHERE item_id = #{itemId} AND user_id = #{userId} AND status IN (0, 1)")
    int exists(@Param("itemId") Long itemId, @Param("userId") Long userId);
    
    // 根据失物ID查询申领列表
    @Select("SELECT * FROM lost_item_claims WHERE item_id = #{itemId} ORDER BY create_time DESC")
    List<LostItemClaim> selectByItemId(Long itemId);
    
    // 查询所有待审核的申领（管理员）
    @Select("SELECT * FROM lost_item_claims WHERE status = 0 ORDER BY create_time DESC")
    List<LostItemClaim> selectPendingClaims();
    
    // 根据ID查询申领
    @Select("SELECT * FROM lost_item_claims WHERE id = #{id}")
    LostItemClaim selectById(Long id);
    
    // 更新审核状态
    @Update("UPDATE lost_item_claims SET status = #{status}, audit_reason = #{auditReason}, audit_time = NOW() WHERE id = #{id}")
    int updateAuditStatus(@Param("id") Long id, @Param("status") Integer status, @Param("auditReason") String auditReason);
    
    // 查询用户所有申领记录
    @Select("SELECT * FROM lost_item_claims WHERE user_id = #{userId} ORDER BY create_time DESC")
    List<LostItemClaim> selectByUserId(Long userId);
}

